package com.xiaomaoguai.jdk.lock;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Description :可重入锁最大的作用是避免死锁 我们以自旋锁作为例子http://ifeve.com/java_lock_see4/
 * @FileName: SpinLock.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年11月10日 下午5:28:16
 * @Version:V1.00
 */
public class SpinLock {

	private AtomicReference<Thread> owner = new AtomicReference<>();

	public void lock() {

		Thread current = Thread.currentThread();

		while (!owner.compareAndSet(null, current)) {

		}

	}

	public void unlock() {

		Thread current = Thread.currentThread();

		owner.compareAndSet(current, null);

	}

}
